﻿using System.Web.Mvc;
using FriendStar.Application.BOM;
using FriendStar.Code;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;

namespace FriendStar.Web.Areas.BOM.Controllers
{
    public class BlankingcardController : ControllerBase
    {
        private string UseUnit = Configs.GetValue("UseUnit");

        BlankingcardApp app = new BlankingcardApp();

        DrawReport dr = new DrawReport();

        public ActionResult BlankingcardManagement()
        {
            return View();
        }

        public ActionResult BlankingcardAdd()
        {
            return View();
        }

        public ActionResult GetBlankingcards(Pagination pagination, string MatNO, string FProductionNO, string FProductionName, string MaterialCodeAll, string MaterialSizes_SJBL, bool isArchive)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetBlankingcards(pagination, MatNO, FProductionNO, FProductionName, MaterialCodeAll, MaterialSizes_SJBL, useUnit, isArchive),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult GetBlankingcardAdds(Pagination pagination, string ProductionNO, string MaterialCodeAll, string MaterialSizesAll, string IsBZGY, string IsImg, string RoughsType)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetBlankingcardAdds(pagination, ProductionNO, MaterialCodeAll, MaterialSizesAll, IsBZGY, IsImg, RoughsType, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult BlankingcardDetails(string id, string archive)
        {
            bool isArchive = archive == "1";

            ViewData["jsonData"] = app.GetBlankingcard(id, isArchive).ToJson();

            ViewData["id"] = id;

            return View();
        }

        public ActionResult GetPreferredPicture(string id, string archive)
        {
            bool isArchive = "1" == archive;

            Dictionary<string, object> obj = (Dictionary<string, object>)app.GetBlankingcard(id, isArchive);

            string MaterialSizes_S = obj["MaterialSizes_SJBL"].ToString();

            string MaterialSizes_J = obj["MaterialSizes_J"].ToString();

            Image img = new Bitmap(930, 437);

            Bitmap bmp = new Bitmap(img.Width, img.Height);

            dr.g = Graphics.FromImage(bmp);

            string path = string.Format("/Content/Temp/{0}.jpg", System.DateTime.Now.ToString("yyyyMMddHHmmss"));

            string realPath = Server.MapPath(path);

            Dictionary<string, string> dict = new Dictionary<string, string>();

            string type = string.Empty;

            dict.Add("块料", "K");
            dict.Add("条料", "T");
            dict.Add("卷料", "C");

            try
            {
                type = dict[obj["RoughsType"].ToString()];
            }
            catch
            {
                type = "N";
            }

            dr.Draw(img, MaterialSizes_S, MaterialSizes_J, obj["RoughsSize"].ToString(), type, realPath);

            MemoryStream ms = new MemoryStream();

            bmp.Save(ms, ImageFormat.Jpeg);

            return File(ms.ToArray(), "image/jpeg");
        }

        public ActionResult BlankingcardLayout(string FProductionNO)
        {
            return View();
        }

        public ActionResult BlankingcardArtPic(string id,string archive)
        {
            bool isArchive = "1" == archive;

            ViewData["domain"] = HttpContext.Request.UrlReferrer.Authority;

            ViewData["jsonData"] = app.GetBlankingcard(id,isArchive).ToJson();

            return View();
        }

        public ActionResult GetArtMatCardMaterialsPic(string Pic_NO)
        {
            byte[] bt = app.GetArtMatCardMaterialsPic(Pic_NO);

            return File(bt, "application/autocad", string.Format("{0}.dwg", Pic_NO));
        }

        public ActionResult GetArtMatCardMaterialsImage(string Pic_NO)
        {
            byte[] bt = app.GetArtMatCardMaterialsPicView(Pic_NO);

            string path = string.Format("/Content/temp/{1}_{0}.png", System.DateTime.Now.ToString("yyyyMMddHHmmss"), Pic_NO);

            string realPath = Server.MapPath(path);

            MemoryStream ms = new MemoryStream();

            Image img = null;

            Bitmap bmp = null;

            if (null != bt)
            {
                img = app.ConvertDwgToPng(bt);

                bmp = ImageOperate.CutImageWhitePart(img, 1);

                bmp.Save(ms, ImageFormat.Jpeg);
            }
            else
            {
                realPath = Server.MapPath("/Content/img/notFoundArtPic.png");
            }

            return File(ms.ToArray(), "image/jpeg");
        }

        public ActionResult BlankingcardMaterialBoards(string MaterialCodeALL, string MaterialSizesAllT)
        {

            ViewData["MaterialCode"] = MaterialCodeALL;

            ViewData["MaterialSizesT"] = MaterialSizesAllT;

            return View();
        }

        public ActionResult GetBlankingcardMaterialBoards(Pagination pagination, string MaterialCode, string MaterialSizesT)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetBlankingcardMaterialBoards(pagination, MaterialCode, MaterialSizesT, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        public ActionResult GetBlankingcardLayoutSets(Pagination pagination, string FProductionNO)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetBlankingcardLayoutSets(pagination, FProductionNO, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }


        public ActionResult GetBlankcardPrintInfo(string MatNO, bool isArchive)
        {//V_XLK_22_BJ_MatCardListAcco
            string useUnit = Configs.GetValue("UseUnit");

            var data = app.GetBlankcardPrintInfo(MatNO, useUnit, isArchive);

            return Content(data.ToJson());
        }

        public ActionResult FrozenBlankcardItem(string MatNO)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = app.FrozenBlankcardItem(MatNO, useUnit);

            return Content(data.ToString());
        }

        public ActionResult ConfirmLayoutSets(string MatListID)
        {
            var data = app.ConfirmLayoutSets(MatListID);

            return Content(data.ToString());
        }

        [ValidateInput(false)]
        public ActionResult LayoutSet(string item, string ProNoNum, string RoughsSize, string MaterialCode_S, string FProductionNO, string MaterialSizes_SH, string MaterialSizes_SK, string MaterialSizes_SC, string MaterialSizes_JK, string MaterialSizes_JC)
        {
            string useUnit = Configs.GetValue("UseUnit");

            string factoryFlag = Configs.GetValue("FactoryFlag");

            Dictionary<string, object> selected = item.ToObject<Dictionary<string, object>>();

            string curUser = OperatorProvider.Provider.GetCurrent().UserName;

            var data = app.LayoutSet(selected, ProNoNum, RoughsSize, FProductionNO, MaterialCode_S, MaterialSizes_SH, MaterialSizes_SK, MaterialSizes_SC, MaterialSizes_JK, MaterialSizes_JC, useUnit, curUser, factoryFlag);

            Dictionary<string, object> result = new Dictionary<string, object>();

            result.Add("result", data);

            result.Add("FProductionNO", FProductionNO);

            return Content(result.ToJson());
        }


    }
}
